package zz.app.ustbgsmanager.adapters;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import zz.app.ustbgsmanager.R;
import zz.app.ustbgsmanager.activities.MainActivity;
import zz.app.ustbgsmanager.beans.queryscore.Score;

/**
 * Created by zz on 2018/6/3 21:46.
 */
public class ScoreListAdapter extends RecyclerView.Adapter<ScoreListAdapter.ScoreListViewHolder> {

    private static final String TAG = "ScoreListAdapter";
    private ArrayList<Score> scores;
    private Context context;
    private AlertDialog dialog = null;

    public ScoreListAdapter(ArrayList<Score> scores, Context context) {
        this.scores = scores;
        this.context = context;
    }


    @NonNull
    @Override
    public ScoreListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_course_score, parent, false);
        ScoreListViewHolder holder = new ScoreListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ScoreListViewHolder holder, int position) {
        holder.tvName.setText(scores.get(position).getCourseName());
        holder.tvScore.setText(scores.get(position).getCourseScore());
        holder.tvID.setText(scores.get(position).getCourseId());

        holder.llContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.llContainer.setEnabled(false);
                int pos = holder.getAdapterPosition();
                Score score = scores.get(pos);
                Log.d(TAG, "pos = " + pos);
                AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.DialogTheme_CourseScoreDetail));
                final View view = View.inflate(context, R.layout.layout_course_score_detail, null);
                TextView tvName = view.findViewById(R.id.tvName);
                TextView tvTerm = view.findViewById(R.id.tvTerm);
                TextView tvCredits = view.findViewById(R.id.tvCredits);
                TextView tvScore = view.findViewById(R.id.tvScore);
                TextView tvId = view.findViewById(R.id.tvID);
                tvName.setText(score.getCourseName());
                tvId.setText(score.getCourseId());
                tvTerm.setText(score.getCourseTerm());
                tvCredits.setText(score.getCourseCredits());
                tvScore.setText(score.getCourseScore());
                final RelativeLayout layout = view.findViewById(R.id.container);
                builder.setView(view);
                dialog = builder.create();
                dialog.show();
                dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                    @Override
                    public void onDismiss(DialogInterface dialog) {
                        holder.llContainer.setEnabled(true);
                    }
                });
                Window window = dialog.getWindow();
                if (window != null) {
                    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    MainActivity activity = (MainActivity) context;
                    Point outPoint = new Point();
                    activity.getWindowManager().getDefaultDisplay().getSize(outPoint);
                    WindowManager.LayoutParams attr = window.getAttributes();
                    attr.width = outPoint.x;
                    attr.height = outPoint.y;
                    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(attr.width, attr.height);
                    view.setLayoutParams(layoutParams);
                    window.setAttributes(attr);
                }
                ValueAnimator valueAnimator=new ValueAnimator();
                valueAnimator.setFloatValues(6,-3,-0.15f);
                valueAnimator.setDuration(500);
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float value= (float) animation.getAnimatedValue();
                        view.setRotationX(value);
                    }
                });
                valueAnimator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        layout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (dialog != null) {
                                    dialog.dismiss();
                                }
                            }
                        });
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                });
                valueAnimator.start();

            }
        });
        if (scores.get(position).getCourseScore().equals("免修")) {
            holder.tvScore.setTextColor(Color.argb(255, 0, 192, 0));
        }
        try {
            int score = Integer.parseInt(scores.get(position).getCourseScore());
            if (score < 60) {
                holder.llContainer.setBackground(new ColorDrawable(Color.argb(128, 255, 0, 0)));
            }
        } catch (Exception ignored) {

        }
    }

    @Override
    public int getItemCount() {
        return scores.size();
    }

    class ScoreListViewHolder extends RecyclerView.ViewHolder {
        public TextView tvName;
        public TextView tvScore;
        public TextView tvID;
        public LinearLayout llContainer;

        public ScoreListViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvScore = itemView.findViewById(R.id.tvScore);
            tvID = itemView.findViewById(R.id.tvID);
            llContainer = itemView.findViewById(R.id.llContainer);
        }
    }
}
